package edu.cuit.svsp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.cuit.svsp.entity.Video;
import edu.cuit.svsp.mapper.VideoMapper;
import edu.cuit.svsp.request.ResPage;
import edu.cuit.svsp.service.VideoService;
import edu.cuit.svsp.util.WebSocketUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.websocket.Session;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @className: VideoServiceImpl
 * @author: pjy
 * @description: 视频服务实现类
 * @date: 2024/7/3
 */
@Slf4j
@Service
public class VideoServiceImpl implements VideoService {
    @Resource
    private VideoMapper videoMapper;

    @Override
    public ResPage<List<Video>> pageByUid(Integer page, Integer size, Integer uid) {
        PageHelper.startPage(page, size);
        List<Video> videos = videoMapper.findByUid(uid);
        PageInfo<Video> pageInfo = new PageInfo<>(videos);
//        log.info("pageInfo:{}",pageInfo);
        ResPage<List<Video>> resPage = new ResPage<>();
        resPage.setData(videos);
        resPage.setPage(page);
        resPage.setSize(size);
        resPage.setTotal(pageInfo.getTotal());
        resPage.setPageTotal(pageInfo.getPages());
        return resPage;
    }

    @Transactional
    @Override
    public boolean addVideo(Video video) {
        // 添加视频信息
        boolean videoFlag = videoMapper.addVideo(video);
        // 添加视频分类信息
        boolean categoryFlag = videoMapper.addVideoCid(video);
        if (videoFlag && categoryFlag) {
            return true;
        }
        return false;
    }

    @Override
    public void addTaskId(Integer id, String taskId) {
        videoMapper.addTaskId(id, taskId);
    }

    @Override
    public List<Video> findByStateCommit() {
        List<Video> list = videoMapper.finfindByStateCommit();
        return list;
    }

    @Override
    public void updateState(Video video) {
        videoMapper.updateState(video);
    }

    @Override
    public List<Video> findNew() {
        return videoMapper.findNew();
    }

    @Override
    public Video findById(Integer id) {
        return videoMapper.findById(id);
    }

    @Transactional
    @Override
    public boolean addPlayNums(Integer vid) {
        return videoMapper.addPlayNums(vid);
    }

    @Override
    public ResPage<List<Video>> findAll(int page, int size) {
        PageHelper.startPage(page, size);
        List<Video> videos = videoMapper.findAll();
        PageInfo<Video> pageInfo = new PageInfo<>(videos);
        ResPage<List<Video>> resPage = new ResPage<>();
        resPage.setData(videos);
        resPage.setPage(page);
        resPage.setSize(size);
        resPage.setTotal(pageInfo.getTotal());
        resPage.setPageTotal(pageInfo.getPages());
        return resPage;
    }


    @Transactional
    @Override
    public boolean lock(Integer id) {
        boolean lock = videoMapper.lock(id);

        //通知up主视频锁定
        String account = videoMapper.findAccountByVid(id);
        String title = videoMapper.findTitleById(id);
        Date currentTime = new Date();
        String message = account + "您的视频:《" + title + "》被管理员锁定" + currentTime.toLocaleString() + "，请及时处理";
        Session session = WebSocketUtil.MESSAGEMAP.get(account);
        WebSocketUtil.sendMessage(session, message);
        return lock;
    }

    @Override
    public boolean pass(Integer id) {
        String account = videoMapper.findAccountByVid(id);
        String title = videoMapper.findTitleById(id);
        Date currentTime = new Date();
        String message = account + "您的视频:《" + title + "》已经审核通过" + currentTime.toLocaleString() + "，请及时处理";
        Session session = WebSocketUtil.MESSAGEMAP.get(account);
        WebSocketUtil.sendMessage(session, message);
        return videoMapper.pass(id);
    }

    @Override
    public ResPage<List<Video>> findByState(int page, int size, String state) {
        if (state.equals("all")) {
            return findAll(page, size);
        } else {
            PageHelper.startPage(page, size);
            List<Video> videos = videoMapper.findByState(state);
            PageInfo<Video> pageInfo = new PageInfo<>(videos);
            ResPage<List<Video>> resPage = new ResPage<>();
            resPage.setData(videos);
            resPage.setPage(page);
            resPage.setSize(size);
            resPage.setTotal(pageInfo.getTotal());
            resPage.setPageTotal(pageInfo.getPages());
            return resPage;
        }
    }

    @Override
    public ResPage<List<Video>> search(int page, int size, String input, String state) {
        PageHelper.startPage(page, size);
        List<Video> videos=new ArrayList<>();
        if (state.equals("all")||state.equals("")) {
            videos = videoMapper.searchAll(input);
        } else {
            videos = videoMapper.searchByState(state, input);
        }
        PageInfo<Video> pageInfo = new PageInfo<>(videos);
        ResPage<List<Video>> resPage = new ResPage<>();
        resPage.setData(videos);
        resPage.setPage(page);
        resPage.setSize(size);
        resPage.setTotal(pageInfo.getTotal());
        resPage.setPageTotal(pageInfo.getPages());
        return resPage;
    }
}
